<template>
	<view class="feedback-container">
		<!-- 反馈内容输入框 -->
		<view class="feedback-content">
			<textarea
				v-model="form.content"
				placeholder="请输入您的意见或建议"
				:maxlength="500"
				class="content-textarea"
			></textarea>
			<text class="word-count">{{form.content.length}}/500</text>
		</view>
		
		<!-- 图片上传区域 -->
		<view class="upload-section">
			<view class="upload-title">上传图片（选填，最多4张）</view>
			<view class="upload-list">
				<view 
					class="upload-item" 
					v-for="(item, index) in imageList" 
					:key="index"
				>
					<image 
						:src="item" 
						mode="aspectFill" 
						class="preview-image"
						@tap="previewImage(index)"
					></image>
					<text 
						class="delete-icon"
						@tap="deleteImage(index)"
					>×</text>
				</view>
				<view 
					class="upload-add"
					v-if="imageList.length < 4"
					@tap="chooseImage"
				>
					<text class="add-icon">+</text>
				</view>
			</view>
		</view>
		
		<!-- 手机号输入框 -->
		<view class="phone-section">
			<view class="phone-title">联系方式（选填）</view>
			<input
				v-model="form.phone"
				type="number"
				maxlength="11"
				placeholder="请输入手机号码"
				class="phone-input"
			/>
		</view>
		
		<!-- 提交按钮 -->
		<view class="submit-btn" @tap="submitFeedback">
			提交反馈
		</view>
	</view>
</template>

<script>
import { addFeedback } from '@/api/system/feedback'
import { upload } from '@/api/common'
import request from '@/utils/request'
import config from '@/config/config'

export default {
	data() {
		return {
			// 表单数据
			form: {
				content: '',
				images: '',
				phone: ''
			},
			// 图片列表
			imageList: []
		}
	},
	methods: {
		// 选择图片
		chooseImage() {
			uni.chooseImage({
				count: 4 - this.imageList.length,
				sizeType: ['compressed'],
				sourceType: ['album', 'camera'],
				success: (res) => {
					// 上传图片
					res.tempFilePaths.forEach(path => {
						this.uploadImage(path)
					})
				}
			})
		},
		// 上传图片
		async uploadImage(filePath) {
			try {
				uni.showLoading({
					title: '上传中...',
					mask: true
				})
				
				// 发送上传请求
				uni.uploadFile({
					url: 'http://localhost/dev-api/common/upload',
					filePath: filePath,
					name: 'file',
					header: {
						'Authorization': uni.getStorageSync('token')
					},
					success: (uploadRes) => {
						try {
							const res = JSON.parse(uploadRes.data)
							if (res.code === 200) {
								// 使用完整的图片URL
								const imageUrl = 'http://localhost/dev-api' + res.url
								this.imageList.push(imageUrl)
								this.form.images = this.imageList.join(',')
								
								uni.showToast({
									title: '上传成功',
									icon: 'success'
								})
							} else {
								uni.showToast({
									title: res.msg || '上传失败',
									icon: 'none'
								})
							}
						} catch (e) {
							console.error('解析响应失败:', e)
							uni.showToast({
								title: '上传失败',
								icon: 'none'
							})
						}
					},
					fail: (err) => {
						console.error('上传失败:', err)
						uni.showToast({
							title: '上传失败',
							icon: 'none'
						})
					},
					complete: () => {
						uni.hideLoading()
					}
				})
			} catch (error) {
				console.error('上传异常:', error)
				uni.hideLoading()
				uni.showToast({
					title: '上传失败',
					icon: 'none'
				})
			}
		},
		// 预览图片
		previewImage(index) {
			uni.previewImage({
				urls: this.imageList,
				current: index
			})
		},
		// 删除图片
		deleteImage(index) {
			this.imageList.splice(index, 1)
			this.form.images = this.imageList.join(',')
		},
		// 提交反馈
		async submitFeedback() {
			// 验证内容
			if (!this.form.content) {
				uni.showToast({
					title: '请输入反馈内容',
					icon: 'none'
				})
				return
			}
			
			// 验证手机号
			if (this.form.phone && !/^1[3-9]\d{9}$/.test(this.form.phone)) {
				uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				})
				return
			}
			
			// 显示加载提示
			uni.showLoading({
				title: '提交中...',
				mask: true
			})
			
			try {
				const userInfo = uni.getStorageSync('userInfo')
				if (!userInfo || !userInfo.userId) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 1500)
					return
				}
				
				const res = await addFeedback({
					content: this.form.content,
					images: this.form.images,
					phone: this.form.phone,
					userId: userInfo.userId
				})
				
				// 隐藏加载提示
				uni.hideLoading()
				
				if (res.code === 200) {
					// 显示确认对话框
					uni.showModal({
						title: '提交成功',
						content: '感谢您的反馈，是否继续反馈？',
						confirmText: '继续反馈',
						cancelText: '返回上页',
						success: (res) => {
							if (res.confirm) {
								// 清空表单
								this.form = {
									content: '',
									images: '',
									phone: ''
								}
								// 清空图片列表
								this.imageList = []
								// 显示提示
								uni.showToast({
									title: '表单已重置',
									icon: 'none'
								})
							} else {
								// 返回上一页
								uni.navigateBack()
							}
						}
					})
				}
			} catch (error) {
				// 隐藏加载提示
				uni.hideLoading()
				
				console.error('提交异常:', error)
				uni.showToast({
					title: error.msg || '提交失败，请稍后重试',
					icon: 'none'
				})
			}
		}
	}
}
</script>

<style>
.feedback-container {
	min-height: 100vh;
	background-color: #f8f8f8;
	padding: 20rpx;
}

.feedback-content {
	background-color: #fff;
	padding: 20rpx;
	border-radius: 12rpx;
	position: relative;
}

.content-textarea {
	width: 100%;
	height: 300rpx;
	font-size: 28rpx;
	line-height: 1.5;
}

.word-count {
	position: absolute;
	right: 20rpx;
	bottom: 20rpx;
	font-size: 24rpx;
	color: #999;
}

.upload-section {
	margin-top: 20rpx;
	background-color: #fff;
	padding: 20rpx;
	border-radius: 12rpx;
}

.upload-title {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 20rpx;
}

.upload-list {
	display: flex;
	flex-wrap: wrap;
}

.upload-item {
	width: 160rpx;
	height: 160rpx;
	margin-right: 20rpx;
	margin-bottom: 20rpx;
	position: relative;
}

.preview-image {
	width: 100%;
	height: 100%;
	border-radius: 8rpx;
}

.delete-icon {
	position: absolute;
	top: -16rpx;
	right: -16rpx;
	width: 32rpx;
	height: 32rpx;
	line-height: 32rpx;
	text-align: center;
	background-color: rgba(0, 0, 0, 0.5);
	color: #fff;
	border-radius: 50%;
	font-size: 24rpx;
}

.upload-add {
	width: 160rpx;
	height: 160rpx;
	border: 2rpx dashed #ddd;
	border-radius: 8rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.add-icon {
	font-size: 48rpx;
	color: #999;
}

.phone-section {
	margin-top: 20rpx;
	background-color: #fff;
	padding: 20rpx;
	border-radius: 12rpx;
}

.phone-title {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 20rpx;
}

.phone-input {
	font-size: 28rpx;
	height: 80rpx;
	border-bottom: 1rpx solid #f5f5f5;
}

.submit-btn {
	margin-top: 40rpx;
	height: 88rpx;
	line-height: 88rpx;
	text-align: center;
	background-color: #409EFF;
	color: #fff;
	border-radius: 44rpx;
	font-size: 32rpx;
}
</style> 